package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseCategoryMapper;
import com.ruoyi.base.domain.BaseCategory;
import com.ruoyi.base.service.IBaseCategoryService;

import javax.annotation.PostConstruct;

/**
 * 资产分类Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-12-20
 */
@Service
public class BaseCategoryServiceImpl implements IBaseCategoryService 
{
    public static final String CACHE_MAP = "base_category";
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private BaseCategoryMapper baseCategoryMapper;


    @PostConstruct
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 0;
    }

    /**
     * 查询资产分类
     *
     * @param name 资产分类名称
     * @return 资产分类
     */
    @Override
    public BaseCategory selectBaseCategoryByName(String name){
        String key = "name_"+name;
        Long cateId = redisCache.getCacheMapValue(CACHE_MAP, name);
        if(cateId != null){
            return selectBaseCategoryById(cateId);
        }

        BaseCategory baseCategory = baseCategoryMapper.selectBaseCategoryByName(name);
        if(baseCategory != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseCategory.getId());
            redisCache.setCacheMapValue(CACHE_MAP, "id_"+baseCategory.getId(), baseCategory);
        }
        return baseCategory;
    }

    /**
     * 查询资产分类
     * 
     * @param id 资产分类主键
     * @return 资产分类
     */
    @Override
    public BaseCategory selectBaseCategoryById(Long id)
    {
        String key = "id_"+id;
        BaseCategory baseCategory = redisCache.getCacheMapValue(CACHE_MAP, key);
        if(baseCategory != null){
            return baseCategory;
        }
        baseCategory = baseCategoryMapper.selectBaseCategoryById(id);

        if(baseCategory != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseCategory);
            redisCache.setCacheMapValue(CACHE_MAP, "name_"+baseCategory.getName(), baseCategory.getId());
        }
        return baseCategory;
    }

    /**
     * 查询资产分类列表
     * 
     * @param baseCategory 资产分类
     * @return 资产分类
     */
    @Override
    public List<BaseCategory> selectBaseCategoryList(BaseCategory baseCategory)
    {
        return baseCategoryMapper.selectBaseCategoryList(baseCategory);
    }

    /**
     * 新增资产分类
     * 
     * @param baseCategory 资产分类
     * @return 结果
     */
    @Override
    public int insertBaseCategory(BaseCategory baseCategory)
    {
        BaseCategory old = selectBaseCategoryByName(baseCategory.getName());
        if(old != null){

        }
        BaseCategory info = baseCategoryMapper.selectBaseCategoryById(baseCategory.getPid());
        if(info != null) {
            baseCategory.setAncestors(info.getAncestors() + "," + baseCategory.getParentId());
        }else{
            baseCategory.setAncestors("0");
        }
        baseCategory.setCreateTime(DateUtils.getNowDate());
        return baseCategoryMapper.insertBaseCategory(baseCategory);
    }

    /**
     * 修改资产分类
     * 
     * @param baseCategory 资产分类
     * @return 结果
     */
    @Override
    public int updateBaseCategory(BaseCategory baseCategory)
    {
        BaseCategory newParent = baseCategoryMapper.selectBaseCategoryById(baseCategory.getPid());
        BaseCategory oldCate = baseCategoryMapper.selectBaseCategoryById(baseCategory.getId());
        if (StringUtils.isNotNull(newParent) && StringUtils.isNotNull(oldCate))
        {
            String newAncestors = newParent.getAncestors() + "," + newParent.getId();
            String oldAncestors = oldCate.getAncestors();
            baseCategory.setAncestors(newAncestors);
            updateDeptChildren(baseCategory.getId(), newAncestors, oldAncestors);
        }

        baseCategory.setUpdateTime(DateUtils.getNowDate());
        return baseCategoryMapper.updateBaseCategory(baseCategory) + reCache();
    }

    /**
     * 修改子元素关系
     *
     * @param cateId 被修改的分类ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateDeptChildren(Long cateId, String newAncestors, String oldAncestors)
    {
        List<BaseCategory> children = baseCategoryMapper.selectChildrenById(cateId);
        for (BaseCategory child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            baseCategoryMapper.updateChildren(children);
        }
    }

    /**
     * 批量删除资产分类
     * 
     * @param ids 需要删除的资产分类主键
     * @return 结果
     */
    @Override
    public int deleteBaseCategoryByIds(Long[] ids)
    {
        if(baseCategoryMapper.checkBaseCategoryDelete(ids)>0){
            throw new ServiceException("有资产的分类不能删除");
        }
        return baseCategoryMapper.deleteBaseCategoryByIds(ids) + reCache();
    }

    /**
     * 删除资产分类信息
     * 
     * @param id 资产分类主键
     * @return 结果
     */
    @Override
    public int deleteBaseCategoryById(Long id)
    {
        return baseCategoryMapper.deleteBaseCategoryById(id) + reCache();
    }
}
